/**
 * API请求的模块化处理
 */
const URLS = {
  hotUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items',
  latestUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items',
  freeUrl: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_free_stream/items',
  detailUrl: 'https://m.douban.com/rexxar/api/v2/movie/'
}

const loadHotFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.hotUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      res.data.method='loadHotFilms'
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadLatestFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.latestUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      res.data.method='loadLatestFilms'
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadFreeFilms = function(params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.freeUrl,
      data: params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      res.data.method='loadFreeFilms'
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadFilmByFilmId = function(filmId){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.detailUrl + filmId,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadActors = function(filmId){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.detailUrl + filmId + '/credits',
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}

const loadComments = function(filmId,params={}){
  // 返回Promise对象
  return new Promise((resolve,reject) => {
    wx.request({
      url: URLS.detailUrl + filmId + '/interests',
      data:params,
      success: resolve,
      fail: reject
    })
  }).then(res => {
    if(res.statusCode==200){
      return res.data
    }else{
      // 将状态由resolve转换为reject
      return Promise.reject({
        message:res.errMsg
      })
    }
  })
}



// 导出
module.exports = {
  loadHotFilms,
  loadLatestFilms,
  loadFreeFilms,
  loadFilmByFilmId,
  loadActors,
  loadComments
}